module.exports = (app) => {
    const BaseController = require('./base')(app);
    return class ProjectController extends BaseController {
        /**
         * 获取项目配置
         */
        get(ctx) {
            const { proj_key: projKey } = ctx.request.query;

            const { project: projectService } = app.service;
            const projConfig = projectService.get({ projKey });

            if (!projConfig) {
                return this.fail('获取项目异常',50000);
            }

            return this.success(ctx, projConfig);
        }
        /**
         * 获取项目列表
         */
         getList(ctx) {
            const { proj_key: projKey }  = ctx.request.query;
  
            const { project: projectService } = app.service;
            const projectList =  projectService.getList({ projKey });

            const dtoProjectList = projectList.map(project => {
                const { modelKey, key, name, desc, homePage } = project;
                return {
                    modelKey,
                    key,
                    name,
                    desc,
                    homePage
                }
            })

            this.success(ctx, dtoProjectList);
        }
        /**
         * 获取模型列表
         */
        async getModelList(ctx) {
            const { project: projectService } = app.service;
            const modelList = await projectService.getModelList();

            // 构造返回结果，只返回关键数据
            const dtoModeList = modelList.reduce((preList, item) => {
                const { model, project } = item;

                // 构造 model 关键数据
                const { key, name, desc } = model;
                const dtoModel = { key, name, desc };

                // 构造 project 关键数据
                const dtoProject = Object.keys(project).reduce((dto, projKey) => {
                    const { name, desc, key, homePage } = project[projKey];
                    dto[projKey] = { name, desc, key, homePage };
                    return dto;
                }, {});

                // 构造整合返回结构
                preList.push({
                    model: dtoModel,
                    project: dtoProject 
                })
                return preList;
            }, [])
            this.success(ctx, dtoModeList);
        }
    };
}